package com.tester.zc.test002.runnable;

/**
 * 生产者
 *
 * @Author zhoucan
 * @Date 2022/12/11
 */
public class Producer implements Runnable {

    private int num = 0;

    @Override
    public void run() {
        while (true) {
            synchronized (Constant.object) {
                //执行5次后退出
                int max = 5;
                if ( num == max) {
                    System.exit(0);
                }
                if (Constant.flag) {
                    System.out.println(Thread.currentThread().getName() + "生产了一个包子");
                    Constant.flag = false;
                    num ++;
                    try {
                        //等待唤醒
                        Constant.object.wait();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }

}
